var app = getApp()
var bmap = require('../../../../lib/bmap-wx.min.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    var zid = options.zid
    var type = options.type
    console.log(type)
    this.loadInfo();
    var BMap = new bmap.BMapWX({
      ak: '4DGFO0htsrocLEd7iQefj7F9tL1Fw1Xn'
    });
    var fail = function(data) {
      console.log(data)
    };
    var success = function(data) {
      console.log(data)
      // wxMarkerData = data.wxMarkerData;
      wxMarkerData = that.data.array
      that.setData({
        markers: wxMarkerData,
      });
    }
    that.setData({
      zid: zid,
      type: type
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  loadInfo: function() {
    var page = this
    wx.getLocation({
      type: 'wgs84', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标  
      success: function(res) {
        console.log(res)
        var longitude = res.longitude
        var latitude = res.latitude
        page.loadCity(longitude, latitude)
        page.getAllzhuanjia(longitude, latitude)
      },
      fail: function() {
        // fail  
      },
      complete: function() {
        // complete  
      }
    })
  },
  loadCity: function(longitude, latitude) {
    var page = this
    wx.request({
      url: 'https://api.map.baidu.com/geocoder/v2/?ak=4DGFO0htsrocLEd7iQefj7F9tL1Fw1Xn&location=' + latitude + ',' + longitude + '&output=json',
      data: {},
      header: {
        'Content-Type': 'application/json'
      },
      success: function(res) {
        console.log(res);
        var city = res.data.result.addressComponent.city;
        //查询当前城市的所有活动
        var lat = res.data.result.location.lat
        var lng = res.data.result.location.lng
        var sematic_description = res.data.result.sematic_description
        page.setData({
          city: city,
          lat: lat,
          lng: lng,
          sematic_description: sematic_description
        });

      },
      fail: function() {
        // fail  
      },
      complete: function() {
        // complete  
      }
    })
  },
  distance: function(la1, lo1, la2, lo2) {
    var La1 = la1 * Math.PI / 180.0;
    var La2 = la2 * Math.PI / 180.0;
    var La3 = La1 - La2;
    var Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;
    var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(Lb3 / 2), 2)));
    s = s * 6378.137; //地球半径
    s = Math.round(s * 10000) / 10000;
    console.log("计算结果", s)
    this.setData({
      s: s
    })
  },
  getAllzhuanjia: function(longitude, latitude) {
    var that = this
    var zid = that.data.zid
    app.util.request({
      'url': 'entry/wxapp/Zhuanjiajinw',
      data: {
        jingdu: longitude,
        latitude: latitude,
        zid: zid,
        op: 'post'
      },
      success: function(res) {
        console.log(res)
        var info = res.data.data
        that.distance(latitude, longitude, info.lat, info.lng)
        info.juli = that.data.s;
        that.setData({
          zhuanjia: info
        })
      },
      fail: function(err) {
        console.log(err)
      },
    })
  },
  OK: function(e) {
    var that = this
    var zhuanjia = e.currentTarget.dataset.zhuanjia
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 3];
    console.log(prevPage)
    prevPage.setData({
      zhuanjia: zhuanjia,
      perfect: 'doc'
    })
    wx.navigateBack({
      delta:2
    })
  }
})